import type { Commit } from 'vuex';
import { getSourceList } from '@/api/plan.ts';

interface SourceItem {
    label: string;
    value: string;
}
interface PlanState {
    selectedCity: null | number;
    planId: number | null;
    orderId: number | null;
    isEdit: boolean | null;
    sourceList: SourceItem[];
    showCancelDisable: boolean;
    deleted: boolean;
    curMediaType: number;
}

function getDefaultState(): PlanState {
    return {
        orderId: null,
        selectedCity: null,
        planId: null,
        isEdit: false,
        sourceList: [],
        showCancelDisable: false,
        deleted: false,
        curMediaType: 1,
    };
}

const state = getDefaultState();

const mutations = {
    setPlanId(state: PlanState, planId: number) {
        state.planId = planId;
    },
    setOrderId(state: PlanState, orderId: any) {
        sessionStorage.setItem('orderId', orderId);
        state.orderId = orderId;
    },
    setIsEdit(state: PlanState, isEdit: boolean) {
        state.isEdit = isEdit;
    },
    setSelectedCity(state: PlanState, cityCode: number) {
        state.planId = cityCode;
    },
    setSourceList(state: PlanState, sourceList: Array<SourceItem>) {
        state.sourceList = sourceList;
    },
    setShowCancelDisable(state: PlanState, showCancelDisable: boolean) {
        state.showCancelDisable = showCancelDisable;
    },
    setDeleted(state: PlanState, deleted: boolean) {
        state.deleted = deleted;
    },
    setCurMediaType(state: PlanState, mediaType: number) {
        state.curMediaType = mediaType;
    },
};
const actions = {
    async getSourceList({ commit }: { commit: Commit }) {
        const data = await getSourceList();
        const result = data.map(({ code, name }: { code: string; name: string }) => ({
            label: name,
            value: code,
        }));
        commit('setSourceList', result);
    },
};
const getters = {};

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters,
};
